package com.zmn.oms.task.job.report;

import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.spring.tasktracker.JobRunnerItem;
import com.github.ltsopensource.spring.tasktracker.LTS;
import com.github.ltsopensource.tasktracker.Result;
import com.zmn.common.dto.ResultDTO;
import com.zmn.manager.lts.interfaces.task.TaskManager;
import com.zmn.oms.business.interfaces.report.ReportCoupAnalyseBService;
import com.zmn.oms.manager.utils.TaskConsts;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;


/**
 * 优惠卷分析报表数据job
 *
 * @author: HuangYongJie
 * @version: v1.0
 * @since: 2019/11/6 10:54
 **/
@LTS
@Slf4j
public class ReportCoupAnalyseJob {

    private static final String REPORT_COUP_ANALYSE_JOB_CRON_TIME = "0 0 5 * * ?";

    @Resource
    TaskManager taskManager;

    @Autowired
    private ReportCoupAnalyseBService reportCoupAnalyseBService;


    /**
     * 提交任务
     */
    @PostConstruct
    void init() {
        Job job = new Job();
        job.setTaskId(TaskConsts.TASK_ID_REPORT_COUP_ANALYSE_JOB);
        job.setParam(TaskManager.SHARD_FIELD, TaskConsts.TASK_ID_REPORT_COUP_ANALYSE_JOB);
        job.setCronExpression(REPORT_COUP_ANALYSE_JOB_CRON_TIME);
        //存在则替换
        job.setReplaceOnExist(true);
        ResultDTO result = taskManager.submitTask(job);
        if (!result.getStatusBool()) {
            log.error("#" + TaskConsts.TASK_ID_REPORT_COUP_ANALYSE_JOB + "# job submit fail...");
        }
    }

    @JobRunnerItem(shardValue = TaskConsts.TASK_ID_REPORT_COUP_ANALYSE_JOB)
    public Result runJob(Job job) {
        String result;
        try {
            log.info("synReportCoupAnalyseData start:" + System.currentTimeMillis());
            result = reportCoupAnalyseBService.synReportCoupAnalyseData();
            log.info("synReportCoupAnalyseData end :" + System.currentTimeMillis());
            return new Result(Action.EXECUTE_SUCCESS, result);
        } catch (Exception e) {
            log.error("synReportCoupAnalyseData error:", e);
            return new Result(Action.EXECUTE_LATER, e.getMessage());
        }
    }


}
